package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.ComponentMapper;
import com.think.teaching.model.Component;
import com.think.teaching.service.ComponentService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 视频课程组件 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class ComponentServiceImpl extends BaseServiceImpl<ComponentMapper, Component> implements ComponentService {

	@Override
	public Component AddComponent(Component component) {
		if (this.save(component)) {
			return component;
		}
		return null;
	}


	@Override
	public Component UpdateComponent(Component component) {
		if (this.updateById(component)) {
			return component;
		}
		return null;
	}

	@Override
	public Component GetComponent(Integer id) {
		QueryWrapper<Component> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public List<Component> SearchComponentsWithKey(String key,Boolean withoutBan) {
		QueryWrapper<Component> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("comp_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		return list(queryWrapper);
	}

	@Override
	public IPage<Component> ListComponentWithTagAndType(Integer count, Integer page, Integer type, Integer tag,Boolean withoutBan) {
		QueryWrapper<Component> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<Component> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	@Override
	public Component SetComponentState(Integer id, Integer state) {
		UpdateWrapper<Component> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

}
